
if (typeof(CTI_FILESYS) != "boolean") {
    var CTI_FILESYS = true;
    
    var filesys = {};
    var mozilla_filesys = {};
    var ie_filesys = {};
    
    filesys.copy = function(from,to) {
        if (mozilla_filesys.detect())
            return mozilla_filesys.copy(from,to);
        if (ie_filesys.detect())
            return ie_filesys.copy(from,to);
        return false;
    }
    
    filesys.listing = function(folder,filter) {
        if (mozilla_filesys.detect())
            return mozilla_filesys.listing(folder,filter);
        if (ie_filesys.detect())
            return ie_filesys.listing(folder,filter);
        return false;
    }
    
    filesys.writefile = function(path,content) {
        if (mozilla_filesys.detect())
            return mozilla_filesys.writefile(path,content);
        if (ie_filesys.detect())
            return ie_filesys.writefile(path,content);
        return false;
    }
    
    filesys.readfile = function(path) {
        if (mozilla_filesys.detect())
            return mozilla_filesys.readfile(path);
        if (ie_filesys.detect())
            return ie_filesys.readfile(path);
        return false;
    }
    
    filesys.parent = function(path) {
            var pos = path.lastIndexOf("\\");
            if(pos==-1)
                pos = path.lastIndexOf("/");
            return path.substring(0,pos+1);
    }
    
    filesys.leaf = function(path) {
            var pos = to.lastIndexOf("\\");
            if(pos==-1)
                pos = to.lastIndexOf("/");
            return to.substring(pos+2);
    }
    
    mozilla_filesys.detect = function() {
        if(window.Components) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                if (Components.classes['@mozilla.org/file/local;1']) {
                    filesys.copy = mozilla_filesys.copy;
                    filesys.listing = mozilla_filesys.listing;
                    filesys.writefile = mozilla_filesys.writefile;
                    filesys.readfile = mozilla_filesys.readfile;
                    return true;
                }
            } catch(ex) {
                return false;
            }
        }
        return false;
    }
    
    mozilla_filesys.open = function(path) {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        var file = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
        file.initWithPath(path);
        return file;
    }
    
    mozilla_filesys.copy = function(from,to) {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            var file = mozilla_filesys.open(from);
            var folder = filesys.parent();
            var newname = filesys.leaf();
            file.copyTo(folder,newname);
            return true;
        }
        catch(e) {
            return false;
        }
    }
    
    mozilla_filesys.listing = function(folder,filter) {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            var file = mozilla_filesys.open(folder);
            if (!file.isDirectory()) {
                filesys.error = "File is not a Directory";
                return false;
            }
            var list = new Object();
            list.folders = [];
            list.files = [];
            var files = file.directoryEntries;
            while (files.hasMoreElements()) {
                var subfile = files.getNext().QueryInterface(Components.interfaces.nsILocalFile);
                var subname = subfile.leafName;
                if (subfile.isDirectory()) {
                    list.folders.push(subname);
                } else if (filter && subname.match(filter) || !filter) {
                    list.files.push(subname);
                }
            }
            return list;
        } catch(ex) {
            filesys.error = "Filesys Exception: "+ex.name+": "+ex.message;
            return false;
        }
    }
    
    mozilla_filesys.writefile = function(path,content) {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            var file = mozilla_filesys.open(path);
            if (!file.exists())
                file.create(0,0664);
            var out = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
            out.init(file,0x20|0x02,00004,null);
            out.write(content,content.length);
            out.flush();
            out.close();
            return true;
        } catch(ex) {
            filesys.error = "Filesys Exception: "+ex.name+": "+ex.message;
            return false;
        }
    }
    
    mozilla_filesys.readfile = function(path) {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			var file = mozilla_filesys.open(path);
			if (!file.exists())
				return false;
			var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
			inputStream.init(file,0x01,00004,null);
			var sInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
			sInputStream.init(inputStream);
			var content = sInputStream.read(sInputStream.available());
			sInputStream.close();
			inputStream.close();
			return content;
		} catch(ex) {
			return false;
		}
    }
    
    ie_filesys.detect = function() {
        try {
            ie_filesys.fso = new ActiveXObject("Scripting.FileSystemObject");
            if (ie_filesys.fso) {
                filesys.copy = ie_filesys.copy;
                filesys.listing = ie_filesys.listing;
                filesys.writefile = ie_filesys.writefile;
                filesys.readfile = ie_filesys.readfile;
                return true;
            }
        } catch(ex) {
            return false;
        }
        return false;
    }
    
    ie_filesys.copy = function(from,to) {
        try {
            // Build directory structure if necessary
            var scan = [filesys.parent(to)];
            var parent = ie_filesys.fso.GetParentFolderName(path);
            while (parent && !ie_filesys.fso.FolderExists(parent)) {
                scan.push(parent);
                parent = ie_filesys.fso.GetParentFolderName(parent);
            }
            for (i = scan.length-1; i >= 0; i--) {
                if (!ie_filesys.fso.FolderExists(scan[i])) {
                    ie_filesys.fso.CreateFolder(scan[i]);
                }
            }
            ie_filesys.fso.GetFile(from).Copy(to);
            return true;
        } catch(ex) {
            return false;
        }
    }
    
    ie_filesys.listing = function(folder,filter) {
        try {
            var dir = ie_filesys.fso.GetFolder(folder);
            var list = new Object();
            list.folders = [];
            list.files = [];
            var subdirs = new Enumerator(dir.SubFolders);
            for (;!subdirs.atEnd(); subdirs.moveNext()) {
                list.folders.push(subdirs.item().Name);
            }
            var files = new Enumerator(dir.Files);
            for (;!files.atEnd(); files.moveNext()) {
                var subname = files.item().Name;
                if (filter && subname.match(filter) || !filter)
                    list.files.push(subname);
            }
            return list;
        } catch(ex) {
            return false;
        }
    }
    
    ie_filesys.writefile = function(path,contents) {
        try {
            // Build directory structure if necessary
            var scan = [filesys.parent(path)];
            var parent = ie_filesys.fso.GetParentFolderName(path);
            while (parent && !ie_filesys.fso.FolderExists(parent)) {
                scan.push(parent);
                parent = ie_filesys.fso.GetParentFolderName(parent);
            }
            for (i = scan.length-1; i >= 0; i--) {
                if (!ie_filesys.fso.FolderExists(scan[i])) {
                    ie_filesys.fso.CreateFolder(scan[i]);
                }
            }
            // Now actually create the file and write the contents
            var file = ie_filesys.fso.OpenTextFile(filePath,2,-1,0);
            file.Write(content);
            file.Close();
            return true;
        } catch(ex) {
            return false;
        }
    }
    
    ie_filesys.readfile = function(path) {
        try {
            var file = ie_filesys.fso.OpenTextFile(path,1);
            var content = file.ReadAll();
            file.Close();
            return content;
        } catch(ex) {
            return false;
        }
    }
}
